﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;

namespace BullShitPokerServer
{
    class Client
    {

        // Конструктор класса. Ему нужно передавать принятого клиента от TcpListener
        public Client(TcpClient Client, IGame game)
        {
            StreamReader sr = new StreamReader(Client.GetStream());
            StreamWriter sw = new StreamWriter(Client.GetStream());
            // Объявим строку, в которой будет хранится запрос клиента
            string Request = sr.ReadLine().Replace('\a', '\n');
            string Answer = "";
            char[] sep = { ' ' };
            string[] Input = Request.Split('\n')[1].Split(sep, 2);
            string id = "";
            try
            {
                id = Input[1];
            }
            catch { }

            switch (Input[0])
            {
                case "Ping":
                    Answer = "Pong!";
                    break;
                case "CountAll":
                    Answer = game.CountCardsAll.ToString();
                    break;
                case "MyCards":
                    Answer = game.MyCards(id);
                    break;
                case "Register":
                    Answer = game.AddPlayer(id).ToString();
                    break;
                case "Remove":
                    Answer = game.RemovePlayer(id).ToString();
                    break;
                case "Chat":
                    Answer = game.Chat;
                    break;
                case "Round":
                    Answer = game.Round.ToString();
                    break;
                case "Move":
                    Answer = game.Move(id);
                    break;
                case "No":
                    Answer = game.No(id).ToString();
                    break;
                case "NextPlayer":
                    Answer = game.NextMovePlayer;
                    break;
                case "RoundStarted":
                    Answer = game.RoundStarted.ToString();
                    break;
                case "LastMove":
                    Answer = game.LastMove;
                    break;
                case "Say":
                    Answer = game.Say(Request.Split('\n')[0], id).ToString();
                    break;
                default:
                    goto case "Ping";
            }
            Client.GetStream().Write(Encoding.UTF8.GetBytes(Answer.Replace('\n', '\a')), 0, Answer.Length);
            Client.Close();
            Console.WriteLine(Request + " // " + Answer);
            //sw.WriteLine(Answer.Replace('\n', '\a'));
                       
        }
    }

}
